package com.androturka.billintur.cyberpark;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import org.w3c.dom.NodeList;


import android.app.Dialog;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;
import com.androturka.cyberpark.R;

public class YemekhaneActivity extends TabActivity {


	String tarih 		= null;
	String corba 		= null;
	String anayemek1 	= null;
	String vejeteryan 	= null;
	String anayemek2 	= null;
	String pilav 		= null;
	String makarnaborek = null;
	String zeytinyagli 	= null;
	String tatli1 		= null;
	String tatli2 		= null;
	String meyve 		= null;
	String salata 		= null;

	
	private ListView listViewGunluk;
	private ListView listViewHaftalik;
	private ListView listViewAylik;

    TabHost tabHost;
    NodeList nodeListMenu = null; 

	private YemekhaneDB DB;

	int menuCount;
	int visibleMonthlyPosition;
	int visibleWeeklyPosition;
    
	final String urlStr = "http://www.depremgunlugu.com/yemekhane/menu.xml";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //DB = new YemekhaneDB(getApplicationContext());
    	//dbObject = DB.getWritableDatabase();		
    	//DB.onCreate(dbObject);
        
    	DB = YemekhaneDB.getInstance(getApplicationContext());

    	
		tabHost=getTabHost();
		tabHost.setup();

		TabSpec spec0=tabHost.newTabSpec("Tab 0");
		spec0.setContent(R.id.tabGunluk);
		spec0.setIndicator("Bugün",getResources().getDrawable(R.drawable.today));

		TabSpec spec1=tabHost.newTabSpec("Tab 1");
		spec1.setContent(R.id.tabHaftalik);
		spec1.setIndicator("Bu Hafta",getResources().getDrawable(R.drawable.week));

		TabSpec spec2=tabHost.newTabSpec("Tab 2");
		spec2.setContent(R.id.tabAylik);
		spec2.setIndicator("Bu Ay",getResources().getDrawable(R.drawable.month));


		tabHost.addTab(spec0);
		tabHost.addTab(spec1);
		tabHost.addTab(spec2);
        
        listViewGunluk=(ListView) findViewById(R.id.listViewGunluk);
        listViewHaftalik=(ListView) findViewById(R.id.listViewHaftalik);
        listViewAylik=(ListView) findViewById(R.id.listViewAylik);
        try {
			prepareApplicationScreen();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
	private void prepareApplicationScreen() throws ParseException  {

		MenuDownloader downloader = new MenuDownloader(this);
		downloader.download();

		ArrayList<DailyMenu> menuArray;
		
		menuArray = DB.getMenus();
		menuCount = menuArray.size();
		Log.d(Common.TAG, "number of menu found " + menuCount );
		
		DailyMenu[] menuAylik = new DailyMenu[menuCount];
		DailyMenu[] menuHaftalik = new DailyMenu[5];
		DailyMenu[] menuGunluk = new DailyMenu[1];
		
		for ( int index = 0; index < menuArray.size(); index++)
		{
			DailyMenu menu = menuArray.get(index);

			if ( corba != null)
				menu.setIsWorkDay(true);
			else
				menu.setIsWorkDay(false);
				
			menuAylik[index] = menu;
			
			Date date = Common.convertStrToDate(menu.getTarih());
			if ( date != null ) {					
				if ( Common.isinSameWeek( date ) )
					menuHaftalik[Common.getDayOfWeek(date)-2] = menu;
				
				if ( Common.isToday( date )){
					menuGunluk[0] = menu;
					menu.setIsToday(true);
				}
				else if ( Common.isPastDate( date ) ) {
					menu.setIsPastDay(true);
				}
				else {
					menu.setIsUpcomingDay(true);
				}
				
				if ( Common.isNextMonth( date )) {
					menu.setIsNextMonth(true);
				}
			}	

		}	
		
		downloader.setAlarm();

		MenuAdapter menuAylikAdapter = new MenuAdapter( getApplicationContext(), menuAylik );			
		listViewAylik.setAdapter(menuAylikAdapter);
		
		MenuAdapter menuHaftalikAdapter = new MenuAdapter( getApplicationContext(), menuHaftalik );			
		listViewHaftalik.setAdapter(menuHaftalikAdapter);
		
		MenuAdapter menuGunlukAdapter = new MenuAdapter( getApplicationContext(), menuGunluk );			
		listViewGunluk.setAdapter(menuGunlukAdapter);
					
		listViewAylik.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {


				int visibility;
									
				for ( int i = 0; i < menuCount; i++ )
				{
					if ( i != position )
					{
						View otherView = listViewAylik.getChildAt(i);
						
						if ( otherView != null )
						{
							LinearLayout otherLayout = (LinearLayout) otherView.findViewById(R.id.panel);
							visibility = otherLayout.getVisibility();
							if ( visibility == View.VISIBLE )
							{	
								otherLayout.setVisibility(View.GONE);
								ImageView image = (ImageView) otherView.findViewById(R.id.collapseView);
								image.setImageResource(R.drawable.expand);
							}
						}								
					}							
				}	
				
				LinearLayout layout = (LinearLayout) view.findViewById(R.id.panel);

				visibility = layout.getVisibility();
				//Log.d("test", "position : " + position + " visibility : " + visibility);
				if (( visibility == View.VISIBLE ) || 
				    ( visibleMonthlyPosition == position ))
				{	
					//Log.d("test", "setVisibility GONE");
					
					layout.setVisibility(View.GONE);

					ImageView image = (ImageView) view.findViewById(R.id.collapseView);
					image.setImageResource(R.drawable.expand);
					visibleMonthlyPosition = -1;
				}		
				else
				{	
					visibleMonthlyPosition = position;
					//Log.d("test", "setVisibility VISIBLE");
					
					layout.setVisibility(View.VISIBLE);

					ImageView image = (ImageView) view.findViewById(R.id.collapseView);
					image.setImageResource(R.drawable.collapse);
				}	
			}
		});
		

		listViewHaftalik.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				int visibility;
									
				for ( int i = 0; i < 5; i++ )
				{
					if ( i != position )
					{
						View otherView = listViewHaftalik.getChildAt(i);
						
						if ( otherView != null )
						{
							LinearLayout otherLayout = (LinearLayout) otherView.findViewById(R.id.panel);
							visibility = otherLayout.getVisibility();
							if ( visibility == View.VISIBLE )
							{	
								otherLayout.setVisibility(View.GONE);
								ImageView image = (ImageView) otherView.findViewById(R.id.collapseView);
								image.setImageResource(R.drawable.expand);
							}
						}								
					}							
				}	
				
				LinearLayout layout = (LinearLayout) view.findViewById(R.id.panel);

				visibility = layout.getVisibility();
				if (( visibility == View.VISIBLE ) || 
					( visibleWeeklyPosition == position ))
				{	
					//Log.d("test", "setVisibility GONE");
					
					layout.setVisibility(View.GONE);

					ImageView image = (ImageView) view.findViewById(R.id.collapseView);
					image.setImageResource(R.drawable.expand);
					visibleWeeklyPosition = -1;
				}		
				else
				{	
					visibleWeeklyPosition = position;
					//Log.d("test", "setVisibility VISIBLE");
					
					layout.setVisibility(View.VISIBLE);

					ImageView image = (ImageView) view.findViewById(R.id.collapseView);
					image.setImageResource(R.drawable.collapse);
				}	
			}
		});			

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.menu, menu);
		return true;

	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
		final Dialog dialog = new Dialog(this);
		
		switch (item.getItemId()) {
			case R.id.hakkinda:
	
				dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.about);
				dialog.show();
	
				Button positiveButton = (Button) dialog
						.findViewById(R.id.positiveButton);
	
				positiveButton.setOnClickListener(new OnClickListener() {
	
					@Override
					public void onClick(View arg0) {
						dialog.cancel();
					}
				});
	
				break;
	
			case R.id.refresh:
				Intent refreshIntent = new Intent(getApplicationContext(), YemekhaneActivity.class);
				//refreshIntent.setAction(ACTION_MAIN);
				startActivity(refreshIntent); 

				break;

			case R.id.settings:
				Intent settingsIntent = new Intent(getApplicationContext(), SettingsActivity.class);
				//refreshIntent.setAction(ACTION_MAIN);
				startActivity(settingsIntent); 

				break;
		} //switch


		return super.onOptionsItemSelected(item);
	}
	
	

	@Override
	protected void onStart() {
		super.onStart();
	}

}
